package com.atguigu.my_aop.aspect;

import com.atguigu.my_aop.annotations.CustomerLogAnnotation;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Aspect
@Component
@Slf4j
public class CoustomerLogAspect {
    //新建一个切入点，PointCut往哪切
    //五大通知之环绕通知
    @Around("@annotation(com.atguigu.my_aop.annotations.CustomerLogAnnotation)")
    public Object logAroud(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{

        Object result = null;

        //通过反射获得方法名
        MethodSignature methodSignature = (MethodSignature) proceedingJoinPoint.getSignature();
        Method method = methodSignature.getMethod();

        //容器捞鱼，捞带有自定义注解CustomerLogAnnotation的方法
        CustomerLogAnnotation customerLogAnnotation = method.getAnnotation(CustomerLogAnnotation.class);

        //方法上含有该注解且enable = true才可以
        if (customerLogAnnotation != null && customerLogAnnotation.enable()){


            long startTime = System.currentTimeMillis();
            System.out.println("----开始执行任务" + proceedingJoinPoint.getSignature().getName());


            result = proceedingJoinPoint.proceed();//主业务逻辑放行


            System.out.println("----任务执行结束" + customerLogAnnotation.value());
            long endTime = System.currentTimeMillis();
            System.out.println("---运行时间"+ (endTime - startTime)+"毫秒");


            return result;

        }else {
            return proceedingJoinPoint.proceed();
        }



    }

}
